import { createRouter, createWebHashHistory } from 'vue-router'

const Home = () => import('../pages/Home.vue')
const Login = () => import('../pages/Login.vue')
const ShopCar = () => import('../pages/ShopCar.vue')
const PersonCenter = () => import('../pages/PersonalCenter.vue')
const GoodsDetail = () => import('../pages/GoodsDetail.vue')
const Management = () => import('../pages/Management.vue')

const routes = [
  { path: '/', component: Home },
  { path: '/login', component: Login },
  { path: '/shop-car', component: ShopCar },
  { path: '/person-center', component: PersonCenter },
  { path: '/goods/:id', component: GoodsDetail },
  { path: '/management', component: Management }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// router.beforeEach((to, from, next) => {
//   console.log('路由守卫')
// })

router.beforeEach((to, form, next) => {
  if (to.path == '/login' || to.path == '/') return next()
  const userFlag = window.localStorage.getItem('user_info')
  if (!userFlag) return next('/login') //无值 返回登录页
  next()
})
export default router
